<template>
  <el-table :data="Ad" stripe>
    <el-table-column type="index" width="100"></el-table-column>
    <el-table-column prop="name" label="广告名称"></el-table-column>
    <el-table-column label="幻灯片">
      <template slot-scope="scope">{{scope.row.items.length}}</template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="180">
      <template slot-scope="scope">
        <el-button type="text" size="small"
        @click="$router.push(`/ads/edit/${scope.row._id}`)">编辑</el-button>
        <el-button type="text" size="small"
        @click="remove(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  data () {
    return {
      Ad: []
    }
  },
  methods: {
    async getAd () {
      const { data: res } = await this.$http.get('/rest/ads')
      this.Ad = res
    },
    remove (row) {
      this.$confirm(`是否删除广告 "${row.title}" ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$http.delete(`/rest/ads/${row._id}`)
        this.$message.success('删除成功!')
        this.getAd()
      })
    }
  },
  created () {
    this.getAd()
  }
}
</script>

<style lang="less" scoped>
</style>
